/*************************************************************************************************
 * Class to represent a factory of geometric elements
 *************************************************************************************************/

package model;
import geometricElement.*;
import java.util.HashMap;

public class GeoElementsFactory {
	
	// interface of object factory
	private interface ObjectFac {
		public GeometricElement create(int xCord, int yCord, double property);
	}	
	// circle factory
	private class CircleFac implements ObjectFac {
			public GeometricElement create(int xCord, int yCord, double radius) { return new Circle(xCord, yCord, radius); }
	}
	// square factory
	private class SquareFac implements ObjectFac {
		public GeometricElement create(int xCord, int yCord, double length) { return new Square(xCord, yCord, length); }
	}
	// perfect triangle factory
	private class PerfectTriangleFac implements ObjectFac {
		public GeometricElement create(int xCord, int yCord, double length) { return new PerfectTriangle(xCord, yCord, length); }
	}

	// members	
	private HashMap<String, ObjectFac> objectFactory; // factory for all geometric elements
	
	/*************************************************************************************************
	 * Function name: public GeoElmntFactory()
	 * Input: None
	 * Output: None
	 * Function operation: Constructor- creates a new geometric elements factory- a key is a string holding
	 * 					   element's name and the value is the matching factory
	 *************************************************************************************************/
	public GeoElementsFactory() {
		objectFactory = new HashMap<String, ObjectFac> ();
		objectFactory.put("Circle", new CircleFac());
		objectFactory.put("Square", new SquareFac());
		objectFactory.put("PerfectTriangle", new PerfectTriangleFac());
	}
		
	/*************************************************************************************************
	 * Function name: public GeometricElement createElement(String type, int xCord, int yCord, double property)
	 * Input: Name, position and property of element to be created (for a circle, the property is radius, 
	 *        for a square or a perfect triangle the  property is sides' length
	 * Output: Referenced to the new geometric element
	 * Function operation: Uses given type string in order to find suitable factory in the hash table.
	 *                     Then calls its create method with given shape property.
	 *                     If given key is not in the hash table, NullPointerException is thrown
	 *************************************************************************************************/
	public GeometricElement createElement(String type, int xCord, int yCord, double property) throws NullPointerException {		
		return objectFactory.get(type).create(xCord, yCord, property);
	}
}